
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <string>
#include <WiFi.h>

/**
const char *ssid = "esp32c6";
const char *password = "17366269557";

#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"

bool deviceConnected = false;
bool oldDeviceConnected = false;
uint32_t value = 0;

// 服务器回调函数
class MyServerCallbacks: public BLEServerCallbacks {
    void onConnect(BLEServer* pServer) {
        deviceConnected = true;
        BLEDevice::startAdvertising();
    };
    
    void onDisconnect(BLEServer* pServer) {
        deviceConnected = false;
    }
};

// 特征值回调函数
class MyCallbacks: public BLECharacteristicCallbacks {
    void onWrite(BLECharacteristic* pCharacteristic) {
        String value = pCharacteristic->getValue();
        String data = "";
        
        if (value.length() > 0) {
            Serial.println("*********");
            Serial.print("New value: ");
            for (int i = 0; i < value.length(); i++)
                data += value[i];
            Serial.println(data);
        }
    }
};
**/

void setup() {
    // Serial.begin(115200);
    
    // 初始化BLE设备
    BLEDevice::init("MyESP32");
    WiFi.begin("w2", "17366269557");
    
    // 创建BLE服务器
    // BLEServer* pServer = BLEDevice::createServer();
    // pServer->setCallbacks(new MyServerCallbacks());
    
    // // 创建BLE服务
    // BLEService* pService = pServer->createService(SERVICE_UUID);
    
    // // 创建特征值（支持读写）
    // BLECharacteristic* pCharacteristic = pService->createCharacteristic(
    //     CHARACTERISTIC_UUID,
    //     BLECharacteristic::PROPERTY_READ |
    //     BLECharacteristic::PROPERTY_WRITE
    // );
    
    // // 设置特征值回调
    // pCharacteristic->setCallbacks(new MyCallbacks());
    
    // // 设置初始值
    // pCharacteristic->setValue("Hello World");
    
    // // 启动服务
    // pService->start();
    
    // // 开始广播
    // BLEAdvertising* pAdvertising = pServer->getAdvertising();
    // pAdvertising->start();
    
    // Serial.println("BLE服务已启动，等待连接...");
}

void loop() {
    // 处理设备连接状态变化
    // if (deviceConnected && !oldDeviceConnected) {
    //     oldDeviceConnected = deviceConnected;
    //     Serial.println("设备已连接");
    // }
    
    // if (!deviceConnected && oldDeviceConnected) {
    //     oldDeviceConnected = deviceConnected;
    //     Serial.println("设备已断开");
    // }
    
    delay(2000);
}
